#include <bits/stdc++.h>
using namespace std;
int t,n,a[20002];
vector<vector<long long> >dp;
map<int,int> zer;
long long ans;
int main()
{
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	scanf("%d",&t);
	while(t--)
	{
		zer.clear();
		ans=0;
		scanf("%d",&n);
		dp.resize(n+2);
		dp[0].resize(n+2);
		dp[0][0]=0;
		for(int i=1;i<=n;++i)
		{
			dp[i].resize(n+2);
			scanf("%d",a+i);
			for(int j=0;j<i;j++)
			{
				dp[j][i]=dp[j][i-1];
				dp[i][j]=dp[i-1][j];
				if(zer[a[i]] && zer[a[i]]!=j)
				{
					dp[j][i]=max(dp[j][i],dp[i-1][zer[a[i]]]+a[i]);
					dp[i][j]=max(dp[i][j],dp[zer[a[i]]][i-1]+a[i]);
				}
				ans=max(ans,max(dp[i][j],dp[j][i]));
			}
			zer[a[i]]=i;
		}
		printf("%lld\n",ans);
	}
	return 0;
} 
